<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    button.active {
      color: white;
      background-color: orangered;
    }
  </style>
</head>
<body>
  <canvas id="c1" width="800" height="600"></canvas>
  <hr>
  <button id="boldBtn" type="button">粗线条</button>
  <button id="thinBtn" type="button">细线条</button>
  <button id="saveBtn" type="button">保持签名</button>
  <input type="color" id="color" value="" />
  <button id="clearBtn" type="button">橡皮擦</button>
  <button id="nullBtn" type="button">清空画布</button>

  <script>
    var canvas = document.querySelector('#c1')
    var cvsCtx = canvas.getContext('2d')
    cvsCtx.lineJoin = 'round';
    cvsCtx.lineCap = 'round';

    var boldBtn = document.querySelector('#boldBtn')
    var thinBtn = document.querySelector('#thinBtn')
    var saveBtn = document.querySelector('#saveBtn')
    var inputColor = document.querySelector('#color')
    var clearBtn = document.querySelector('#clearBtn')
    var nullBtn = document.querySelector('#nullBtn')

    // 设置是否运行绘制
    var isDraw = false

    canvas.onmousedown = (event) => {
      isDraw = true
      cvsCtx.beginPath();

      var x = event.pageX - canvas.offsetLeft
      var y = event.pageY - canvas.offsetTop

      cvsCtx.moveTo(x, y);

    }

    canvas.onmouseleave = () =>{
      isDraw = false
      cvsCtx.closePath();
    }

    canvas.onmouseup = () => {
      isDraw = false
      cvsCtx.closePath();
    }

    canvas.onmousemove = (event) => {
      if(!isDraw) return
      var x = event.pageX - canvas.offsetLeft
      var y = event.pageY - canvas.offsetTop
      cvsCtx.lineTo(x, y);
      cvsCtx.stroke();
    }

    boldBtn.onclick = () =>{
      cvsCtx.globalCompositeOperation = 'source-over';
      cvsCtx.lineWidth = 20;
      boldBtn.classList.add('active')
      thinBtn.classList.remove('active')
      clearBtn.classList.remove('active')
    }
    thinBtn.onclick = () =>{
      cvsCtx.globalCompositeOperation = 'source-over';
      cvsCtx.lineWidth = 2;
      thinBtn.classList.add('active')
      boldBtn.classList.remove('active')
      clearBtn.classList.remove('active')
    }

    clearBtn.onclick = () => {
      cvsCtx.globalCompositeOperation = 'destination-out';
      cvsCtx.lineWidth = 30;
      clearBtn.classList.add('active')
      boldBtn.classList.remove('active')
      thinBtn.classList.remove('active')
    }
    nullBtn.onclick = () => {
      cvsCtx.clearRect(0, 0, 800, 600);
    }
    saveBtn.onclick = () => {
      var urlData = canvas.toDataURL()
      var img = new Image()
      img.src = urlData
      document.body.appendChild(img)
      
      var downloadA = document.createElement('a')
      downloadA.setAttribute('download','签名')
      downloadA.href = urlData
      downloadA.click()
    }
    inputColor.onchange = () => {
      // cvsCtx.fillStyle = ;
      console.log('e',inputColor.value);
      cvsCtx.strokeStyle = inputColor.value;
    }
  </script>
</body>
</html>